package com.sptpc.demo.controller;

import com.sptpc.demo.model.Category;
import com.sptpc.demo.model.Drink;
import com.sptpc.demo.model.Student;
import com.sptpc.demo.service.ICategoryService;
import com.sptpc.demo.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;

@Controller
public class RouterController {
    @Autowired
    public IDrinkService drinkService;
    @Autowired
    public ICategoryService categoryService;

    //首页登陆路由
    @GetMapping("/")
    public String Index(){

        return "manage/login";
    }
    //后台首页路由
    @GetMapping("/manage/index")
    public String adminIndex(){
        return "manage/index";
    }
    //后台欢迎路由
    @GetMapping("/manage/welcome")
    public String welcome(){

        return "manage/welcome";
    }
    //饮品管理页
    @GetMapping("/menu/drink")
    public String drinkIndex(){

        return "drink/index";
    }
    //学生管理界面
    @GetMapping("/menu/student")
    public String student(){
        return "student/index";
    }
    //饮品添加路由
    @GetMapping("/drink/addform")
    public String drinkAddIndex(){

        return "drink/add";
    }
    //添加学生
    @GetMapping("/student/addform")
    public String addstudent(){
        return "/student/add";
    }
    //饮品修改路由
    @GetMapping("/drink/updateform")
    public ModelAndView drinkUpdateForm(@RequestParam Integer id){
        ModelAndView mv=new ModelAndView("drink/edit");
        //TODO: 从数据库通过id查询出对应的饮品信息
        Drink drink = drinkService.getDrinkById(id);

        mv.addObject("data",drink);
        return mv;
    }
    //修改学生
    //get /drink/updatafrom?id=1
    @GetMapping("/student/updateform")
    public ModelAndView updatedeletestudent(@RequestParam Long id){
        ModelAndView mv = new ModelAndView("student/edit");
        //ToDD:从数据库中查询出对应的商品信息
        Student build = Student.builder()
                .id(3L)
                .name("张三")
                .adminssionDate(LocalDate.parse("2001-02-05"))
                .major("开发").build();
        mv.addObject("data",build);
        return mv;
    }

    //分类管理页
    @GetMapping("/menu/category")
    public String categoryIndex(){
        return "category/index";
    }
    //分类添加页路由
    @GetMapping("/category/addform")
    public String categoryAddFrom(){
        return "category/add";
    }
    //get /category/updateform?id=2
    @GetMapping("/category/updateform")
    public ModelAndView categoryUpdateForm(@RequestParam Integer id){
        ModelAndView mv=new ModelAndView("category/edit");
        Drink drink = drinkService.getDrinkById(id);
        Category category = categoryService.getCategoryById(id);

        mv.addObject("data",category);
        return mv;
    }
}
